/*
* $Id: ChangeQBWnd.cpp 134 2010-08-26 11:32:19Z volok-aleksej@yandex.ru $
*
* Copyright 2010 by Alexey Volokitin
* All rights reserved
*
*/
#include "Common/include.h"
#include "ChangeQBWnd.h"
#include "Components/Journal/QueenBeeManager.h"
#include "Components/Journal/JournalInterface.h"
#include "Database/Tables/FamiliesTable.h"
#include "../BQManagerDialog.h"

#define STATIC_WIDTH		120

CChangeQBWnd::CChangeQBWnd(bool bCreate){
	m_pBrush.CreateSolidBrush(RGB(255, 255, 255));
	m_bCreate = bCreate;
}
CChangeQBWnd::~CChangeQBWnd(){
	m_pBrush.DeleteObject();
}

void CChangeQBWnd::WindowCreate(CWnd* pParent){
	CreateEx(WS_EX_CONTROLPARENT, AfxRegisterWndClass(CS_HREDRAW| CS_VREDRAW, LoadCursor(NULL, IDC_ARROW), m_pBrush),
		_T(""), WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP, RECT_RIGHT_WIN, pParent, RIGHT_WIN_ID);
}

void CChangeQBWnd::WindowDestroy(){
	DestroyWindow();
}

void CChangeQBWnd::SetQueenBeeId(int nId){
	__super::SetQueenBeeId(nId);
	if(!m_bCreate){
		CQueenBeeManager::queen_bee qb = IJournalComponent::GetJournalComponent()->GetQueenBeeManager()->GetQueenBee(m_nId);
		m_Name.SetWindowText(qb.s_strName.c_str());
		CTime ctime(qb.s_date);
		m_Date.SetTime(&ctime);
		m_Breed.SetCurSel(qb.s_nBreed);
		m_Exist.SetCheck(!qb.s_bDead);
		m_Fert.SetWindowText(qb.s_strFert.c_str());
		m_Descr.SetWindowText(qb.s_strDescription.c_str());
	}
	Invalidate();
}

void CChangeQBWnd::SetSelectionData(unsigned long data){
	__super::SetSelectionData(data);
	m_nIdFamily = 0;
	if(LOWORD(m_selData) == CBQManagerDialog::FAMILY){
		m_nIdFamily = HIWORD(m_selData);
		m_strNameQueenBee = IApplication::GetApp()->GetLanguageManager()->GetString(NO_CURRENT_PARENT_QB_ID);
		m_nIdQueenBee = 0;
	}else if(LOWORD(m_selData) == CBQManagerDialog::QUEEN_BEE){
		CQueenBeeManager::queen_bee qb = IJournalComponent::GetJournalComponent()->GetQueenBeeManager()->GetQueenBee(HIWORD(m_selData));
		m_nIdFamily = qb.s_nIdFamily;
		m_strNameQueenBee = qb.s_strName;
		m_nIdQueenBee = HIWORD(m_selData);
	}else{
		m_strNameQueenBee = IApplication::GetApp()->GetLanguageManager()->GetString(NO_CURRENT_PARENT_QB_ID);
		m_nIdQueenBee = 0;
	}
	if(m_nIdFamily){
		IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
		CFamiliesTable table;
		table.m_content.s_nId = m_nIdFamily;
		IResult* pResult = table.GetValue(pDb);
		m_strNameFamily = ((CFamiliesTable::content*)(*pResult)[0])->s_strName;
		pResult->Free();
		IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
	}else
		m_strNameFamily = IApplication::GetApp()->GetLanguageManager()->GetString(NO_CURRENT_FAMILY_QB_ID);
	Invalidate();
}

void CChangeQBWnd::Save(){
	CString strTemp;
	m_Name.GetWindowText(strTemp);
	std::tstring strName = strTemp.GetString();
	m_Descr.GetWindowText(strTemp);
	std::tstring strDescription = strTemp.GetString();
	m_Fert.GetWindowText(strTemp);
	std::tstring strFert = strTemp.GetString();
	CTime timeDest;
	m_Date.GetTime(timeDest);
	time_t date = timeDest.GetTime();
	bool bExist = m_Exist.GetCheck() == BST_CHECKED;
	int nBread = m_Breed.GetCurSel();
	CQueenBeeManager* pQb = IJournalComponent::GetJournalComponent()->GetQueenBeeManager();
	if(m_bCreate){
		int nIdFamily = m_CurFamily.GetCheck() == BST_CHECKED ? m_nIdFamily : 0;
		int nIdParent = m_CurParent.GetCheck() == BST_CHECKED ? m_nIdQueenBee : 0;
		int nId = pQb->AddQueenBee(strName, strDescription, strFert, date, !bExist, nBread, nIdFamily);
		if(nIdParent)
			pQb->ChangeParentQB(nId, nIdParent);
	}else
		pQb->ChangeQueenBee(m_nId, strName, strDescription, strFert, date, !bExist, nBread);
}

BEGIN_MESSAGE_MAP(CChangeQBWnd, CWnd)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_GETDLGCODE()
END_MESSAGE_MAP()

int CChangeQBWnd::OnCreate(LPCREATESTRUCT lpCreateStruct){
	int nRes = __super::OnCreate(lpCreateStruct);
	m_Name.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL | WS_TABSTOP,
		CRect(STATIC_WIDTH + 2, 30, lpCreateStruct->cx - 5, 50), this, 0);
	m_Date.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | DTS_SHORTDATEFORMAT | WS_TABSTOP,
		CRect(STATIC_WIDTH + 2, 55, lpCreateStruct->cx - 5, 77), this, 0);
	m_Breed.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_DROPDOWNLIST | WS_TABSTOP,
		CRect(STATIC_WIDTH + 2, 83, lpCreateStruct->cx - 5, 127), this, 0);
	for(int i = 0;; i++){
		m_Breed.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(i + BREED_HYBRID_ID).c_str());
		if(i + BREED_HYBRID_ID == BREED_FAREAST_ID)
			break;
	}
	m_Exist.Create(_T(""), WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | WS_TABSTOP,
		CRect(STATIC_WIDTH + 2, 113, STATIC_WIDTH + 2 + 15, 128), this, 0);
	m_Fert.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | WS_TABSTOP,
		CRect(STATIC_WIDTH + 2, 135, lpCreateStruct->cx - 5, 155), this, 0);
	m_Descr.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_TABSTOP | ES_LEFT | ES_MULTILINE | ES_WANTRETURN,
		CRect(STATIC_WIDTH + 2, 160, lpCreateStruct->cx - 5, 230), this, 0);
	if(m_bCreate){
		m_CurParent.Create(_T(""), WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | WS_TABSTOP,
			CRect(5, 235, 20, 250), this, 0);
		m_CurFamily.Create(_T(""), WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | WS_TABSTOP,
			CRect(5, 280, 20, 295), this, 0);
	}
	return nRes;
}

void CChangeQBWnd::OnPaint(){
	CPaintDC dc(this);
	dc.SetBkMode(TRANSPARENT);
	CFont* pFont = CCommonFunction::GetFont(_T("Arial"), 20, TRUE, TRUE, FALSE);
	HGDIOBJ oldFont = dc.SelectObject(pFont->m_hObject);
	CRect rect;
	GetClientRect(rect);
	dc.DrawText(IApplication::GetApp()->GetLanguageManager()->GetString(
		m_bCreate ? ADD_QUEEN_BEE_ID : UPDATE_QUEEN_BEE_ID).c_str(), rect, DT_CENTER);
	dc.SelectObject(oldFont);
	pFont->DeleteObject();
	delete pFont;
	dc.DrawText(IApplication::GetApp()->GetLanguageManager()->GetString(INFO_INSPECT_NAME_QB_ID).c_str(),
		CRect(0, 30, STATIC_WIDTH, 50), DT_LEFT | DT_SINGLELINE | DT_VCENTER);
	dc.DrawText(IApplication::GetApp()->GetLanguageManager()->GetString(INFO_INSPECT_DATE_QB_ID).c_str(),
		CRect(0, 55, STATIC_WIDTH, 77), DT_LEFT | DT_SINGLELINE | DT_VCENTER);
	dc.DrawText(IApplication::GetApp()->GetLanguageManager()->GetString(INFO_INSPECT_BREED_QB_ID).c_str(),
		CRect(0, 83, STATIC_WIDTH, 107), DT_LEFT | DT_SINGLELINE | DT_VCENTER);
	dc.DrawText(IApplication::GetApp()->GetLanguageManager()->GetString(INFO_INSPECT_STATUS_QB_ID).c_str(),
		CRect(0, 113, STATIC_WIDTH, 130), DT_LEFT | DT_SINGLELINE | DT_VCENTER);
	dc.DrawText(IApplication::GetApp()->GetLanguageManager()->GetString(INFO_INSPECT_FERT_QB_ID).c_str(),
		CRect(0, 135, STATIC_WIDTH, 155), DT_LEFT | DT_SINGLELINE | DT_VCENTER);
	dc.DrawText(IApplication::GetApp()->GetLanguageManager()->GetString(INFO_INSPECT_DESCR_QB_ID).c_str(),
		CRect(0, 160, STATIC_WIDTH, 230), DT_LEFT | DT_SINGLELINE | DT_VCENTER);

	if(m_bCreate){
		dc.DrawText(IApplication::GetApp()->GetLanguageManager()->GetString(USE_CUR_PARENT_QB_ID).c_str(),
			CRect(20, 235, rect.right, 250), DT_LEFT | DT_SINGLELINE | DT_VCENTER);
		dc.DrawText(IApplication::GetApp()->GetLanguageManager()->GetString(USE_CUR_FAMILY_QB_ID).c_str(),
			CRect(20, 280, rect.right, 295), DT_LEFT | DT_SINGLELINE | DT_VCENTER);
		pFont = CCommonFunction::GetFont(_T("Arial"), 16, FALSE, TRUE, FALSE);
		oldFont = dc.SelectObject(pFont->m_hObject);
		dc.DrawText(m_strNameQueenBee.c_str(), CRect(rect.left, 255, rect.right, 275), DT_CENTER | DT_SINGLELINE | DT_VCENTER);
		dc.DrawText(m_strNameFamily.c_str(), CRect(rect.left, 300, rect.right, 320), DT_CENTER | DT_SINGLELINE | DT_VCENTER);
		dc.SelectObject(oldFont);
		pFont->DeleteObject();
		delete pFont;
	}
}

UINT CChangeQBWnd::OnGetDlgCode(){
	return DLGC_WANTTAB;
}
